﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain.Abstract;
using Domain.Entities;
using WebUI.Models;

namespace WebUI.Controllers
{
    public class CartController : Controller
    {
        private IProductRepository repository;

        public CartController(IProductRepository repo) {
            repository = repo;
        }
        //
        // GET: /Cart/
        public ViewResult Index(Cart cart, string returnUrl)
        {
            return View(new CartIndexViewModel { Cart = cart, returnURL = returnUrl});
        }

        public RedirectToRouteResult AddToCart(Cart cart, int productID, string returnURL) {
            Product product = repository.Products.FirstOrDefault(x => x.ProductID == productID);

            if (product != null) {
                cart.AddItem(product, 1);
            }
            return RedirectToAction("Index", new { returnURL });
        }

        public RedirectToRouteResult RemoveFromCarts(Cart cart, int productId, string returnUrl) {
            Product product = repository.Products.FirstOrDefault(x => x.ProductID == productId);

            if (product != null) {
                cart.RemoveLine(product);
            }
            return RedirectToAction("Index", new { returnUrl });
        }

        public ViewResult Checkout() {
            return View(new ShippingDetails());
        }

        public PartialViewResult Summary(Cart cart) {
            return PartialView(cart);
        }
    }
}
